//
//  LJProfileViewController.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/15.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD

class LJProfileViewController: LJBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        prepareUI()
        calculateCache()
        
    }
    private func prepareUI() {
        view.addSubview(cacheLabel)
        view.addSubview(cleanButton)
        
        cacheLabel.frame = CGRect(x: 30, y: 100, width: 260, height: 40)
        cleanButton.frame = CGRect(x: 30, y: 200, width: 100, height: 40)
    }
    
    private func calculateCache(){
        SDWebImageManager.sharedManager().imageCache.calculateSizeWithCompletionBlock { (conut, cacheSize) -> Void in
//            print("a:\(a),b\(b)")
            self.cacheLabel.text = "缓存大小:\(CGFloat(cacheSize) / 1000 / 1000.0)M"
        }
    }
    
    @objc private func clearCache() {
        print(NSHomeDirectory())
        SDWebImageManager.sharedManager().imageCache.clearDiskOnCompletion { () -> Void in
            SVProgressHUD.showSuccessWithStatus("清理缓存成功")
            self.calculateCache()
        }
    }
    
    //MARK: - 懒加载
    private lazy var cacheLabel: UILabel = UILabel()
    private lazy var cleanButton: UIButton = {
        let button = UIButton()
        button.setTitle("清楚缓存", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "clearCache", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}
